#!/usr/bin/env python
# -*- coding: utf-8 -*-

import time
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains

import utils
from config import CHANNEL_URL


def check_browser(func):
    def wrapper(self, *args, **kwargs):
        if not self._browser:
            print("Failed to get _browser")
        else:
            return func(self, *args, **kwargs)

    return wrapper


class Channel(object):
    def __init__(self):
        self._browser = utils.get_browser()
        print("channel browser %x" % id(self._browser))

    @check_browser
    def create(self):
        self._browser.get(CHANNEL_URL)
        time.sleep(2)

        # 填写直播频道名称：Lancashire
        channel_name = self._browser.find_element_by_name("name")
        channel_name.send_keys("PPC-Lancashire")

        # 设置直播类型：临时直播
        live_type_select = Select(
            self._browser.find_element_by_name('liveType'))
        live_type_select.select_by_index(0)

        # 设置频道类型：临时综艺晚会
        type_select = Select(self._browser.find_element_by_name('type'))
        type_select.select_by_index(1)

        # 填写最大回看时间：24小时
        maxPlaybackPeriod_time = self._browser.find_element_by_name(
            "maxPlaybackPeriod")
        maxPlaybackPeriod_time.clear()
        maxPlaybackPeriod_time.send_keys("24")

        # 设置频道启用状态：是
        isEnable_radios = self._browser.find_elements_by_name('isEnable')
        isEnable_select = isEnable_radios[0]
        isEnable_select.click()

        # 设置是否正式流信号：测试
        formal_radios = self._browser.find_elements_by_name('formal')
        formal_select = formal_radios[1]
        formal_select.click()

        # 填写频道描述：你若精彩，上天自有安排！
        description_text = self._browser.find_element_by_name("description")
        description_text.send_keys(u"你若精彩，上天自有安排！")

        # 设置显示评论：是
        shouldDisplay_radios = self._browser.find_elements_by_name(
            'shouldDisplay')
        shouldDisplay_select = shouldDisplay_radios[0]
        shouldDisplay_select.click()

        # 设置大小写分类：正在直播：临时直播
        category_a = self._browser.find_element_by_id('category-a')
        category_a.click()
        time.sleep(2)

        category_a_select = self._browser.find_element_by_class_name(
            'parent_category')
        for option in category_a_select.find_elements_by_tag_name("option"):
            if option.text == u"正在直播":
                option.click()
                break

        time.sleep(2)

        category_a_select = self._browser.find_element_by_class_name(
            'sub_category')
        for option in category_a_select.find_elements_by_tag_name("option"):
            if option.text == u"临时直播":
                actionChains = ActionChains(self._browser)
                actionChains.double_click(option).perform()
                break

        # 点击确定按钮
        button_div = self._browser.find_element_by_class_name(
            'ui-dialog-buttonset')
        ensure_button = button_div.find_elements_by_tag_name('button')[0]
        ensure_button.click()

        # 上传图片
        upload_img = self._browser.find_element_by_class_name('uploadButton')
        upload_img.send_keys(utils.get_img())
        time.sleep(5)

        wellbox_div = self._browser.find_element_by_class_name('wellbox')
        for button in wellbox_div.find_elements_by_tag_name("button"):
            button.click()
            time.sleep(1)

        # 点击提交按钮
        submit = self._browser.find_element_by_id("Submit")
        submit.click()
        time.sleep(10)

        # 查找新频道名称
        tbody_select = self._browser.find_element_by_tag_name('tbody')
        for tr in tbody_select.find_elements_by_tag_name("tr"):
            links = tr.find_elements_by_tag_name("a")
            if not links:
                continue

            if links[1].text == "PPC-Lancashire":
                father_id = tr.find_elements_by_tag_name('td')[0].text
                return father_id

        #调试断点
        #import pdb
        #pdb.set_trace()
